package com.ruoyi.common.core.config.databind;

import com.alibaba.fastjson2.JSONArray;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * Copyright (c) 2023, gitee
 * All rights reserved.
 *
 * @author qiupt@gitee.com
 * @description jackson后端string转json array对象传递给前端
 * @date 2023/11/09 18:33
 **/
public class StringToJsonArraySerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String str, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (null == str) {
            jsonGenerator.writeObject(null);
            return;
        }
        try {
            jsonGenerator.writeObject(JSONArray.parseArray(str));
        } catch (Exception e) {
            e.printStackTrace();
            jsonGenerator.writeObject(null);
        }
    }
}
